package cn.itcast.tlias.com.tliaswork.controller;

import cn.itcast.tlias.com.tliaswork.pojo.Emp;
import cn.itcast.tlias.com.tliaswork.pojo.PageBean;
import cn.itcast.tlias.com.tliaswork.pojo.Result;
import cn.itcast.tlias.com.tliaswork.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工部门表
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {


    @Autowired
    private EmpService mService;

    /**
     * 查询分页相关
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result search(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询的页是{},查询的条目数是{}", page, pageSize);
        //        PageBean bean = mService.search(page==null?1:page, pageSize==null ? 10:pageSize);
        PageBean bean = mService.page(page, pageSize);

        return Result.success(bean);
    }

    /**
     * 查询分页相关
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/condition")
    public Result searchCondition(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  String name,
                                  Short gender,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询的页是{},查询的条目数是{},姓名{},性别{},开始时间{},结束时间{}", page, pageSize,name,gender,begin,end);
        //        PageBean bean = mService.search(page==null?1:page, pageSize==null ? 10:pageSize);
        PageBean bean = mService.pageCondition(page, pageSize,name,gender,begin,end);

        return Result.success(bean);
    }

    /**
     * 删除多个员工
     * ids 的集合
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除操作{}",ids);
        //        PageBean bean = mService.search(page==null?1:page, pageSize==null ? 10:pageSize);
       // PageBean bean = mService.pageCondition(page, pageSize,name,gender,begin,end);

        mService.delete(ids);

        return Result.success();
    }

    /**
     * 新增员工
     */
    @PostMapping
    public Result addEmp(@RequestBody Emp emp){
        log.info("新增员工的功能：emp {}",emp);
        mService.save(emp);
        return Result.success();
    }

    /**
     * 根据ID查询员工
     */
    @GetMapping("/{id}")
    public Result getEmpMessage(@PathVariable Integer id){
        Emp query = mService.query(id);
        return Result.success(query);
    }
    /**
     * 修改员工信息
     */
    @PutMapping
    public Result upDate(@RequestBody Emp emp){
        log.info("更新员工的信息是:{}", emp);

        mService.updata(emp);
        return Result.success();
    }

}
